<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title data-i18n="resources.title_findMTSPPathsAndTSPPaths"></title>
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            width: 100%;
            height: 100%;
        }

        #toolbar {
            position: absolute;
            top: 50px;
            right: 10px;
            text-align: center;
            z-index: 100;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div id="toolbar" class="panel panel-primary">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.text_findMTSPPathsAndTSPPaths"></h5></div>
    <div class='panel-body content'>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_target" onclick="selectPoints()"/>&nbsp;
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_dispatching"
               onclick="findMTSPPaths()"/>&nbsp;
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_check" onclick="findTSPPaths()"/>&nbsp;
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_clear"
               onclick="clearElements()"/>
    </div>
</div>
<div id="map"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script>
    var host = window.isLocal ? window.server : "https://iserver.supermap.io";
    var map, layer, vectorLayer, markerLayer1,
        markerLayer2, drawPoint, nodeArray = [], i = 0, j = 0,
        centerArray = [new SuperMap.Geometry.Point(6000, -5500),
            new SuperMap.Geometry.Point(5500, -2500),
            new SuperMap.Geometry.Point(2500, -3500)],
        styleMTSP = {
            strokeColor: "#304DBE",
            strokeWidth: 3,
            pointerEvents: "visiblePainted",
            fill: false
        },
        styleTSP = {
            strokeColor: "red",
            strokeWidth: 2,
            //pointRadius: 8,
            pointerEvents: "visiblePainted",
            fill: false
        },
        url1 = host + "/iserver/services/map-changchun/rest/maps/长春市区图",
        url2 = host + "/iserver/services/transportationanalyst-sample/rest/networkanalyst/RoadNet@Changchun";


    init();

    function init() {
        vectorLayer = new SuperMap.Layer.Vector("Vector Layer");
        drawPoint = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Point);
        drawPoint.events.on({"featureadded": drawCompleted});
        map = new SuperMap.Map("map", {
            controls: [
                new SuperMap.Control.ScaleLine(),
                new SuperMap.Control.Zoom(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                }),
                drawPoint], units: "m"
        });
        map.addControl(new SuperMap.Control.LayerSwitcher(), new SuperMap.Pixel(42, 80));
        layer = new SuperMap.Layer.TiledDynamicRESTLayer("Changchun", url1, {
            transparent: true,
            cacheEnabled: true
        }, {maxResolution: "auto"});
        layer.events.on({"layerInitialized": addLayer});
        markerLayer1 = new SuperMap.Layer.Markers("Markers");
        markerLayer2 = new SuperMap.Layer.Markers("Markers");

        var size = new SuperMap.Size(44, 40),
            offset = new SuperMap.Pixel(-(size.w / 2), -size.h);
        markerLayer1.addMarker(new SuperMap.Marker(new SuperMap.LonLat(6000, -5500), new SuperMap.Icon("./images/marker-gold.png", size, offset)));
        markerLayer1.addMarker(new SuperMap.Marker(new SuperMap.LonLat(5500, -2500), new SuperMap.Icon("./images/marker-gold.png", size, offset)));
        markerLayer1.addMarker(new SuperMap.Marker(new SuperMap.LonLat(2500, -3500), new SuperMap.Icon("./images/marker-gold.png", size, offset)));
    }

    function addLayer() {
        map.addLayers([layer, vectorLayer, markerLayer1, markerLayer2]);
        map.setCenter(new SuperMap.LonLat(4503.6240321526, -3861.911472192499), 0);
    }

    function selectPoints() {
        widgets.alert.clearAlert();
        clearElements();
        drawPoint.activate();
    }

    function drawCompleted(drawGeometryArgs) {
        var point = drawGeometryArgs.feature.geometry,
            size = new SuperMap.Size(44, 33),
            offset = new SuperMap.Pixel(-(size.w / 2), -size.h),
            icon = new SuperMap.Icon("./images/marker.png", size, offset);
        markerLayer2.addMarker(new SuperMap.Marker(new SuperMap.LonLat(point.x, point.y), icon));
        nodeArray.push(point);
    }

    function findMTSPPaths() {
        widgets.alert.clearAlert();
        drawPoint.deactivate();
        var findMTSPPathsService, parameter, analystParameter, resultSetting;
        resultSetting = new SuperMap.REST.TransportationAnalystResultSetting({
            returnEdgeFeatures: true,
            returnEdgeGeometry: true,
            returnEdgeIDs: true,
            returnNodeFeatures: true,
            returnNodeGeometry: true,
            returnNodeIDs: true,
            returnPathGuides: true,
            returnRoutes: true
        });
        analystParameter = new SuperMap.REST.TransportationAnalystParameter({
            resultSetting: resultSetting,
            weightFieldName: "length"
        });
        parameter = new SuperMap.REST.FindMTSPPathsParameters({
            centers: centerArray,
            isAnalyzeById: false,
            nodes: nodeArray,
            hasLeastTotalCost: true,
            parameter: analystParameter
        });
        findMTSPPathsService = new SuperMap.REST.FindMTSPPathsService(url2, {
            eventListeners: {"processCompleted": processCompleted1}
        });
        findMTSPPathsService.processAsync(parameter);
    }

    function processCompleted1(findMTSPPathsEventArgs) {
        var result = findMTSPPathsEventArgs.result,
            features = [];
        if (result.pathList) {
            for (var i = 0, pathList = result.pathList, len = pathList.length; i < len; i++) {
                var feature = new SuperMap.Feature.Vector();
                feature.geometry = pathList[i].route;
                feature.style = styleMTSP;
                features.push(feature);
            }
        }
        vectorLayer.addFeatures(features);
    }


    function findTSPPaths() {
        widgets.alert.clearAlert();
        drawPoint.deactivate();
        var findTSPPathsService, parameter, analystParameter, resultSetting;
        resultSetting = new SuperMap.REST.TransportationAnalystResultSetting({
            returnEdgeFeatures: true,
            returnEdgeGeometry: true,
            returnEdgeIDs: true,
            returnNodeFeatures: true,
            returnNodeGeometry: true,
            returnNodeIDs: true,
            returnPathGuides: true,
            returnRoutes: true
        });
        analystParameter = new SuperMap.REST.TransportationAnalystParameter({
            resultSetting: resultSetting,
            weightFieldName: "length"
        });
        parameter = new SuperMap.REST.FindTSPPathsParameters({
            isAnalyzeById: false,
            nodes: nodeArray,
            endNodeAssigned: false,
            parameter: analystParameter
        });
        if (nodeArray.length <= 1) {
            widgets.alert.showAlert(resources.msg_check, false);
        } else {
            findTSPPathsService = new SuperMap.REST.FindTSPPathsService(url2, {
                eventListeners: {"processCompleted": processCompleted2}
            });
            findTSPPathsService.processAsync(parameter);
        }
    }

    function processCompleted2(findTSPPathsEventArgs) {
        var result = findTSPPathsEventArgs.result;
        allScheme(result);
    }

    function allScheme(result) {
        if (i < result.tspPathList.length) {
            addPath(result);
        } else {
            i = 0;
        }
    }

    //以动画效果显示分析结果
    function addPath(result) {
        if (j < result.tspPathList[i].route.components[0].components.length) {
            var pathFeature = new SuperMap.Feature.Vector();
            var points = [];
            for (var k = 0; k < 2; k++) {
                if (result.tspPathList[i].route.components[0].components[j + k]) {
                    points.push(new SuperMap.Geometry.Point(result.tspPathList[i].route.components[0].components[j + k].x, result.tspPathList[i].route.components[0].components[j + k].y));
                }
            }
            var curLine = new SuperMap.Geometry.LinearRing(points);
            pathFeature.geometry = curLine;
            pathFeature.style = styleTSP;
            vectorLayer.addFeatures(pathFeature);
            //每隔0.01毫秒加载一条弧段
            pathTime = setTimeout(function () {
                addPath(result);
            }, 0.01);
            j++;
        } else {
            clearTimeout(pathTime);
            j = 0;
            i++;
            allScheme(result);
        }
    }

    function clearElements() {
        widgets.alert.clearAlert();
        nodeArray = [];
        markerLayer2.clearMarkers();
        vectorLayer.removeAllFeatures();
    }


</script>

</body>
</html>